import 'package:car_map/mode/driving_model.dart';
import 'package:car_map/page/res/gaps.dart';
import 'package:car_map/page/res/styles.dart';
import 'package:car_map/widget/widget_progressbar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:url_launcher/url_launcher.dart';

import 'load_image.dart';

class WidgetDrivingItem extends StatelessWidget {
  final DrivingModel model;

  const WidgetDrivingItem({Key key, this.model}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Card(
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              LoadImage(
                model.urlPath,
                width: ScreenUtil().setWidth(150.0),
                height: ScreenUtil().setHeight(170.0),
              ),
              Container(
                padding: EdgeInsets.only(left: 10.0),
                width: ScreenUtil().setWidth(580.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      '姓名:${model.name}',
                      style: TextStyles.textSize16,
                    ),
                    Gaps.vGap8,
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(
                          '性别:${model.sex}',
                          style: TextStyles.textGray14,
                        ),
                        Text(
                          '年龄:${model.age}',
                          style: TextStyles.textGray14,
                        ),
                      ],
                    ),
                    Gaps.vGap8,
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(
                          '驾龄:${model.drivingAge}',
                          style: TextStyle(color: Colors.blue),
                        ),
                        Text(
                          '公司:${model.company}',
                          style: TextStyles.textGray14,
                        ),
                      ],
                    ),
                    Gaps.vGap8,
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Row(
                          children: <Widget>[
                            Text(
                              '星级:',
                              style: TextStyles.textGray14,
                            ),
                            WidgetProgressBar(
                              width: 120.0,
                              height: 30.0,
                              indexBar: model.starNum,
                              maxLenth: 5,
                              colorsType: Colors.blue,
                            ),
                          ],
                        ),
                        InkWell(
                          child: Icon(
                            Icons.phone_forwarded,
                            color: Colors.blue,
                          ),
                          onTap: (){
                            showAlertDialog(context);
//                            _launchUrl();

                          },
                        )
                      ],
                    )
                  ],
                ),
              )
            ],
          )
        ],
      ),
    );
  }

  //询问dialog
  void showAlertDialog(BuildContext context){
    showDialog(context: context,
        builder: (context) {
          return AlertDialog(title: Text('代驾服务', style: TextStyle(color: Colors.blueAccent)),
              content: Text('联系${model.name}代驾服务'),
              actions: <Widget>[
                FlatButton(child: Text("确定"), onPressed: (){
                  _launchUrl();
                  Navigator.of(context).pop();
                }),
                FlatButton(child: Text("取消"), onPressed: () => Navigator.of(context).pop())
              ]);
        });
  }

  void _launchUrl() async {
    String url = 'tel:' + model.phone;
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw '不能拨打电话';
    }
  }
}
